/**
 * 2018年12月10日下午10:36:28
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.JsonKit;
import com.baihang.framework.kit.PropKit;
import com.baihang.framework.kit.ShiroKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.project.ds.common.CommonService;
import com.baihang.project.ds.entity.DanYuanEntity;
import com.baihang.project.ds.entity.LouDongEntity;
import com.baihang.project.ds.entity.TaoXingEntity;
import com.baihang.project.ds.model.FangYuanModel;
import com.baihang.project.ds.service.FangYuanRedisService;

import redis.clients.jedis.Jedis;

/**
 * 房源
 *
 */
@Controller
@RequestMapping("/fangyuan")
public class FangYuanController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(FangYuanController.class);
	@Autowired
	//private FangYuanService service;
	private FangYuanRedisService service;
	@Autowired
	private CommonService comService;
	private Jedis jedis = RedisKit.getJedis();
	/**
	 * 房源首页
	 * @param model
	 * @return
	 */
	@RequestMapping
	private String index(Model model) {
		logger.info("进入楼栋");
		/**
		 * 当前用户点击具体房源进行详情页时，会将此房源的状态改变预售。其他用户看到此房源时颜色为蓝色。无法再点击
		 * 当用户重新回到列表页时，需要将自己引起的预售改回未受状态，让其他用户可以继续查看。
		 */
		//service.reSetState();//执行一下还原操作
		List<LouDongEntity> list =service.getLouDong();
		model.addAttribute("loudong", list);
		model.addAttribute("ldgd",jedis.hmget("systemConfig","IsShowLDGD").get(0));
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		model.addAttribute("ShoppingTime", jedis.hmget("systemConfig","ShoppingTime").get(0));//抢购时间 
		System.err.println("ShoppingTime="+ jedis.hmget("systemConfig","ShoppingTime").get(0));
		model.addAttribute("alreadyBuyCount", service.alreadyBuyCount());	
		if("0".equals(jedis.hmget("systemConfig","IsTest").get(0))) {
			model.addAttribute("IsTest", "正式活动");
		}else {
			model.addAttribute("IsTest", "测试活动");
		}
		model.addAttribute("imageServer", PropKit.get("imageServer"));
		model.addAttribute("IsShowYHGS", jedis.hmget("systemConfig","IsShowYHGS").get(0));//优惠公示
		
		return "/project/ds/fangyuan/index";
	}
	
	@RequestMapping("/indexData")
	private String indexData(int ldIndex,int dyIndex,Model model) {
		logger.info("进入楼栋");
		/**
		 * 当前用户点击具体房源进行详情页时，会将此房源的状态改变预售。其他用户看到此房源时颜色为蓝色。无法再点击
		 * 当用户重新回到列表页时，需要将自己引起的预售改回未受状态，让其他用户可以继续查看。
		 */
		//service.reSetState();//执行一下还原操作
		List<LouDongEntity> list =service.getLouDong();
		model.addAttribute("loudong", list);
		model.addAttribute("ldgd",jedis.hmget("systemConfig","IsShowLDGD").get(0));
		/*model.addAttribute("StartTime", jedis.hmget("systemConfig","HouseName").get("StartTime"));
		model.addAttribute("ShoppingTime", jedis.hmget("systemConfig","HouseName").get("ShoppingTime"));//抢购时间 
		model.addAttribute("alreadyBuyCount", service.alreadyBuyCount());		*/
		model.addAttribute("ldIndex",ldIndex);//房栋索引
		model.addAttribute("dyIndex",dyIndex);//房源索引
		return "/project/ds/fangyuan/indexData";
	}
	
	@Deprecated
	@RequestMapping("/xiangqing")
	private String xiangqing(int id,Model model) {
		FangYuanModel fyModel =service.getXiangQing(id);
		model.addAttribute("fangyuan", fyModel);
		model.addAttribute("user", ShiroKit.getCurrentUser());
		service.setState(id);//改变状态
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		return "/project/ds/fangyuan/xiangqing";
	}
	@RequestMapping("/xiangqingData")
	private String xiangqingData(int id,String timeState,Model model) {
		FangYuanModel fyModel =service.getXiangQing(id);
		model.addAttribute("imageServer", PropKit.get("imageServer"));
		model.addAttribute("fangyuan", fyModel);
		model.addAttribute("user", ShiroKit.getCurrentUser());
		/*if(!"2".equals(timeState)) {//在项目没有真正开始之前，不改变房源状态
			service.setState(id);//改变状态			
		}*/
		if(comService.checkStart()) {
			model.addAttribute("startColor", "0");//灰色
		}else {
			model.addAttribute("startColor", "1");//橙色
		}
		
		if(comService.checkYuDing(id)) {
			model.addAttribute("yudingColor", "1");
		}
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		model.addAttribute("CollectionCount", jedis.hmget("systemConfig","CollectionCount").get(0));//收藏上限
		//查找收藏的房源id
		model.addAttribute("szIds", service.getShouCangFyIds());
		//选房倒计时时间 单位分钟
		model.addAttribute("CountDownTime", jedis.hmget("systemConfig","CountDownTime").get(0));
		//是否显示价格  在系统配置中显示开启，不做管理帐户的显示控制。只有在显示关闭时，才做管理帐户控制
		if("0".equals(jedis.hmget("systemConfig","IsShowPrice").get(0))) {
			if(jedis.sismember("jiage", ShiroKit.getCurrentUser().getUserName())){
				model.addAttribute("IsShowPrice", "1");
			}
		}else {
			model.addAttribute("IsShowPrice", jedis.hmget("systemConfig","IsShowPrice").get(0));
		}

		return "/project/ds/fangyuan/xiangqingData";
	}
	
	@PostMapping("/danyuanData")
	private String getDanYuan(String name,Model model) {
		List<DanYuanEntity> list = service.getDanYuan(name);
		model.addAttribute("danyuan", list);
		return "/project/ds/fangyuan/danyuanData";
	}
	
	@PostMapping("/taoxingData")
	private String getTaoXing(String loudongName,String danyuanName,Model model) {
		List<TaoXingEntity> list = service.getTaoXing(loudongName, danyuanName);
		model.addAttribute("taoxing", list);
		return "/project/ds/fangyuan/taoxingData";
	}
	
	//选购成功
	@Deprecated
	@GetMapping("/buyRoomForSuccess")
	private String buyRoomForSuccess(int id,Model model) {
		FangYuanModel fyModel =service.getXiangQing(id);
		model.addAttribute("fangyuan", fyModel);
		return "/project/ds/fangyuan/success";
	}
	@GetMapping("/buyRoomForSuccessData")
	private String buyRoomForSuccessData(int id,Model model) {
		FangYuanModel fyModel =service.getXiangQing(id);
		model.addAttribute("fangyuan", fyModel);
		return "/project/ds/fangyuan/successData";
	}
	//选购失败
	@GetMapping("/buyRoomForFail")
	private String buyRoomForFail(Model model) {
		
		Record rec = service.getTuiJian();//获得推荐信息
		model.addAttribute("loudong", rec.getStr("D_BanName"));
		model.addAttribute("danyuan", rec.getStr("D_Unit"));
		model.addAttribute("shihao", rec.getStr("D_RoomNo"));
		model.addAttribute("id", rec.getInt("D_ID"));
		return "/project/ds/fangyuan/fail";
	}
	
	@RequestMapping(value ="/buyRoomForFailJson", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String buyRoomForFailJson(Model model) {	
		String result = "true";
		String msg ="";
		Record rec = service.getTuiJian();//获得推荐信息
		if(rec==null) {
			result="false";
			msg ="没有找到合适的推荐房源";
			return JsonKit.toDoubleJson(result, msg);
		}else {
			msg = JsonKit.toJson(rec);			
			return JsonKit.toDoubleJson2(result, msg);
		}
	}
	
	/**
	 * 通过id买房
	 * @param id
	 * @return
	 */
	@RequestMapping(value ="/buyRoom", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String buyRoom(int id) {
		String result = service.buyRoom(id);
		System.err.println("buyRoom="+result);
		return result;
	}
	
	@PostMapping(value ="/getFangYuanState", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String getFangYuanState(int id) {
		String result = service.getFangYuanState(id);
		return JsonKit.toSingleJson(result);
	}
	
	
	@PostMapping(value ="/getWebSocketIp", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String getWebSocketIp() {
		String result = PropKit.get("websocketIp");
		return JsonKit.toSingleJson(result);
	}
	
	/**
	 * 单个房间进行翻红操作
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value ="/singleFanHong", produces="application/json;charset=UTF-8")
	@ResponseBody
	public String singleFanHong(int id){
		String result ="true" ;
		String msg ="预定中";
		service.singleFanHong(id);
		return JsonKit.toDoubleJson(result, msg);
	}
	
	@PostMapping(value ="/updateHouseState", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String updateHouseState() {
		String result = "false";
		if(service.updateHouseState()) {
			result="true";
		}
		return JsonKit.toSingleJson("true");
	}
	
	/**
	 * 用户预定房间 
	 * true 允许弹出倒计时窗口
	 * false 表示已被其它用户预定
	 * @param id
	 * @return
	 */
	@PostMapping(value ="/userYuDingRoom", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String userYuDingRoom(int roomId) {
		logger.info("{}预定{}房间",ShiroKit.getCurrentUser().getUserName(),roomId);
		return service.userYuDingRoom(roomId);
	}
	
	/**
	 * 还原用户预定的房源状态
	 * @param roomId
	 * @return
	 */
	@PostMapping(value ="/roomResetState", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String roomResetState(int roomId) {
		logger.info("{}退订{}房间",ShiroKit.getCurrentUser().getUserName(),roomId);
		service.reSetState(roomId);
		return JsonKit.toSingleJson("true");
	}
	
}
